package com.gem.service.impl;

import com.gem.domain.*;
import com.gem.mapper.CoreMapper;
import com.gem.service.CoreService;
import com.gem.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-21 21:19
 * @Modified By:
 */
@Service
@SuppressWarnings("all")
@Transactional(rollbackFor=Exception.class)
public class CoreServiceImpl implements CoreService {

    @Autowired
    private CoreMapper mapper;


    @Override
    public Integer savePlan(CorePlan corePlan, Integer campusId) {

        String Gid = UuidUtil.getUuid();

        if (corePlan.getCorePlanLeaderTime() != null) {
//            院校领导来访
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("院校领导来访");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanLeaderTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanStuTime() != null) {
//            学生代表来访
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("学生代表来访");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanStuTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanCourseTime() != null) {
//            课程嵌入
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("课程嵌入");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanCourseTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanLectureTime() != null) {
//            招生讲座
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("招生讲座");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanLectureTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanQqweixinTime() != null) {
//            建立QQ/微信群
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("建立QQ/微信群");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanQqweixinTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanWinterTime() != null) {
//            冬令营
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("冬令营");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanWinterTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        if (corePlan.getCorePlanSummerTime() != null) {
//            夏令营
            CoreEvent coreEvent = new CoreEvent();
            coreEvent.setCoreEventName("夏令营");
            coreEvent.setBeginTime(new Date());
            coreEvent.setPlanTime(corePlan.getCorePlanSummerTime());
            coreEvent.setCampusId(campusId);
            coreEvent.setCoreGroup(Gid);
            mapper.saveEvent(coreEvent);
        }
        Integer result = mapper.savePlan(corePlan);
        return result;
    }


    @Override
    public Integer updateComplete(CoreEvent coreEvent) {
        return mapper.updateComplete(coreEvent);
    }

    @Override
    public PageInfo<CoreEvent> findEventById(Integer campusId, int currentPage, int pageSize) {
        PageInfo<CoreEvent> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(mapper.findEventById(campusId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public Campus findCampus(Integer campusId) {
        return mapper.findCampus(campusId);
    }

    @Override
    public List<CoreEvent> findEvents(Integer campusId) {
        return mapper.findEventById(campusId);
    }

    @Override
    public List<CoreEvent> findEventsByName(String eventName, Integer campusId) {
        return mapper.findEventsByName(eventName, campusId);
    }

    @Override
    public Integer deleteById(Integer coreEventId) {
        return mapper.deleteById(coreEventId);
    }

    @Override
    @Transactional
    public Integer deleteAll(int[] coreEventIds) {
        Integer result = mapper.deleteAll(coreEventIds);
        return result;
    }

    @Override
    public CoreEvent findById(Integer coreEventId) {
        return mapper.findById(coreEventId);
    }

    @Override
    public PageInfo<String> findGroup(Integer campusId, int currentPage, int pageSize) {
        PageInfo<String> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(mapper.findGroup(campusId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CoreEvent> findByGid(String gid, int currentPage, int pageSize) {
        PageInfo<CoreEvent> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(mapper.findByGid(gid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<CoreEvent> findAll() {
        return mapper.findAll();
    }

    @Override
    public Integer deleteByGid(String gid) {
        return mapper.deleteByGid(gid);
    }

    @Override
    public Integer deleteGAll(String[] gids) {
        return mapper.deleteGAll(gids);
    }

    @Override
    public Integer countEvent(Integer uid) {
        return mapper.countEvent(uid);
    }

    @Override
    public Integer countName(Integer uid, String eventName) {
        return mapper.countName(eventName,uid);
    }

    @Override
    public Integer countEventWeek(Integer uid) {
        return mapper.countEventWeek(uid);
    }

    @Override
    public Integer countNameWeek(Integer uid, String eventName) {
        return mapper.countNameWeek(uid,eventName);
    }

}
